// sql.h: interface for the CLSql class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SQL_H__28CC4B40_0EDC_4E46_9694_7AE4B05874DD__INCLUDED_)
#define AFX_SQL_H__28CC4B40_0EDC_4E46_9694_7AE4B05874DD__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "str.h"
#include "file.h"
#include "config.h"

#define	CLSQL_ERR_OTHER 	-4
#define	CLSQL_ERR_RPC 		-3
#define	CLSQL_ERR_DB 		-2
#define CLSQL_ERR_EMPTY		-1
#define	CLSQL_ERR_OK 		0

#define 	DB(x)		(LPCSTR)CLSql::escape(x)
#define 	DBCGI(x)	DB(cgi.param(x))

class CLSql  
{
public:
	CLStr m_error;

	CLSql() {};
	~CLSql() { };

	//virtual function to define general sql function 
	virtual int query(LPCSTR sql,CLStr& result,int with_field=0) { printf("sql err\n"); return CLSQL_ERR_OK; }
	virtual int init(const CLConfig& conf) { return 1; }
	virtual void close() {};

	int query_list(LPCSTR sql,CLStrList& row,int with_field=0);
	int query_row(LPCSTR sql,CLStrList &col);
	int query_val(LPCSTR sql,CLStr& val);
	CLStr query1(LPCSTR sql, LPCSTR def="");
	int   query2(LPCSTR sql, CLStr& v1,CLStr& v2);
	int init(LPCSTR conf_file) { CLConfig conf(conf_file); return init(conf); }

	//static function to split row,col.
	static CLStr row_sep_str;
	static CLStr col_sep_str;
	static int get_row(const CLStr &result, CLStrList &rowlist) { return result.split(row_sep_str,rowlist,1); }
	static int get_col(const CLStr &row, CLStrList &collist) { return row.split(col_sep_str,collist,1); }
	static void list(const CLStr& result,const CLFile& out,int with_field=1);
	static void html_list(const CLStr& result,const CLFile& out,int with_field=1);
	static CLStr escape(CLStr src);
	static CLStr like_escape(CLStr src);
	static void split_page(int total,int pagesize,int pos,int& prev,int& next,int* totalpage=NULL,int* curpage=NULL);
};

#endif // !defined(AFX_SQL_H__28CC4B40_0EDC_4E46_9694_7AE4B05874DD__INCLUDED_)
